package com.itheima.bean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
@WebServlet("/addStudentServlet")
public class AddStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收数据
        String username = req.getParameter("username");
        String age = req.getParameter("age");
        String score = req.getParameter("score");

//封装数据
  Student stu=new Student();
  stu.setUsername(username);
  stu.setAge(Integer.parseInt(age));
  stu.setScore(Integer.parseInt(score));

//将数据写入本地文件
BufferedWriter bw=new BufferedWriter(new FileWriter("e:\\d.txt",true));
bw.write(username+","+age+","+score);
bw.newLine();
bw.close();

//页面跳转
resp.setContentType("text/html;charset=UTF-8");
resp.getWriter().write("添加成功，2秒后自动跳转到首页");
resp.setHeader("Refresh","2;URL=/stu/index.html");



    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
